package org.dromara.learning_xq.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dromara.learning_xq.entity.AsQuestionCondition;
import org.dromara.learning_xq.mapper.AsQuestionConditionMapper;
import org.dromara.learning_xq.service.AsQuestionConditionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AsQuestionConditionServiceimpl extends ServiceImpl<AsQuestionConditionMapper, AsQuestionCondition> implements AsQuestionConditionService {

    @Autowired
    private AsQuestionConditionMapper asQuestionConditionMapper;

    /**
     * 增加作为形成性测试的条件
     *
     * @param asQuestionCondition 要增加的条件对象如果为null，则返回空列表
     * @return 插入成功时返回包含新记录的列表，否则返回空列表
     */
    @Override
    public List<AsQuestionCondition> increaseAsFormativeTest(AsQuestionCondition asQuestionCondition) {
        // 判断传入的对象是否为空
        if (asQuestionCondition == null) {
            // 如果为空，返回空列表
            return Collections.emptyList();
        }

        // 将数据插入到数据库
        int insertResult = asQuestionConditionMapper.insert(asQuestionCondition);

        // 检查插入结果
        if (insertResult > 0) {
            // 插入成功后，返回包含新记录的列表
            return List.of(asQuestionCondition);
        } else {
            // 如果插入失败，返回空列表或其他适当的处理方式
            return Collections.emptyList();
        }
    }

    @Override
    public AsQuestionCondition selectAsChapterContentDtoList() {
        List<AsQuestionCondition> asQuestionConditions = asQuestionConditionMapper.selectList(null);

        Map<Long, AsQuestionCondition> collect = asQuestionConditions.stream()
            .collect(Collectors.toMap(AsQuestionCondition::getId, node -> node));

        AsQuestionCondition root = null;

        for (AsQuestionCondition node : asQuestionConditions) {
            Long pid = node.getPid();
            if (pid == 0) {
                root = node;
            } else {
                AsQuestionCondition asQuestionCondition = collect.get(pid);
                if (asQuestionCondition != null) {
                    if (asQuestionCondition.getChildren() == null) {
                        asQuestionCondition.setChildren(new ArrayList<>());
                    }
                    asQuestionCondition.getChildren().add(node);
                }
            }
        }
        return root;
    }

    @Override
    public List<AsQuestionCondition> updateAsQuestionConditions(AsQuestionCondition asQuestionCondition) {
        // 判断传入的对象是否为空
        if (asQuestionCondition == null) {
            // 如果为空，返回空列表
            return Collections.emptyList();
        }

        // 将数据插入到数据库
        int insertResult = asQuestionConditionMapper.updateById(asQuestionCondition);

        // 检查插入结果
        if (insertResult > 0) {
            // 插入成功后，返回包含新记录的列表
            return List.of(asQuestionCondition);
        } else {
            // 如果插入失败，返回空列表或其他适当的处理方式
            return Collections.emptyList();
        }
    }
}
